package com.lianlian.controller;

import com.github.pagehelper.PageInfo;
import com.lianlian.goods.pojo.Brand;
import com.lianlian.service.BrandService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
@CrossOrigin //跨域问题
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 查询所有品牌的
     */
    @GetMapping
    public Result<List<Brand>> findAll() {
        List<Brand> brands = brandService.findAll();
        //响应结果封装
        return new Result<>(true, StatusCode.OK, "查询品牌集合成功", brands);
    }

    /**
     * 通过Id查询品牌
     */
    @GetMapping(value = "/{id}")
    public Result<Brand> findById(@PathVariable(value = "id") Integer id) {
        Brand brand = brandService.findById(id);
        return new Result<>(true, StatusCode.OK, "根据Id查询成功", brand);
    }

    /**
     * 增加品牌
     */
    @PostMapping
    public Result add(@RequestBody Brand brand) {
        brandService.add(brand);
        return new Result(true, StatusCode.OK, "添加品牌成功");
    }

    /**
     * 品牌修改实现
     */
    @PutMapping(value = "{id}")
    public Result update(@PathVariable(value = "id") Integer id, @RequestBody Brand brand) {
        //将id给Brand
        brand.setId(id);
        //调用service实现增加
        brandService.update(brand);
        //返回
        return new Result(true, StatusCode.OK, "品牌修改成功");
    }

    /**
     * 品牌删除实现
     */
    @DeleteMapping("/{id}")
    public Result delById(@PathVariable("id") Integer id) {
        brandService.delById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @PostMapping("/search")
    public Result<List<Brand>> findlist(@RequestBody Brand brand) {
        List<Brand> list = brandService.findlist(brand);
        return new Result<List<Brand>>(true, StatusCode.OK, "条件搜索查询成功", list);
    }

    /**
     * 分页查询实现
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<Brand>> findPage(@PathVariable(value = "page") Integer page, @PathVariable(value = "size") Integer size) {
        //调用service查询 pageinfo
        PageInfo<Brand> pageinfo = brandService.findPage(page,size);
        //返回结果
        return new Result<PageInfo<Brand>>(true,StatusCode.OK,"分页查询成功",pageinfo);
    }
    /**
     * 条件+分页查询实现
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<Brand>> findPage(@RequestBody Brand brand,@PathVariable(value = "page") Integer page, @PathVariable(value = "size") Integer size) {
        //调用service查询 pageinfo
        PageInfo<Brand> pageinfo = brandService.findPage(brand,page,size);
        //返回结果
        return new Result<PageInfo<Brand>>(true,StatusCode.OK,"分页查询成功",pageinfo);
    }
}
